/*            Copyright(c) 2006-2008 Dave Sexton           *
 * http://www.codeplex.com/DocProject                      *
 * http://www.codeplex.com/DocProject/Project/License.aspx *
 ***********************************************************/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace DaveSexton.DocProject.ExternalUI
{
	internal sealed partial class BuildItemsForm : Form
	{
		#region Public Properties
		public IEnumerable<DocProjectItemBase> Items
		{
			get
			{
				foreach (DocProjectItemBase item in itemsBindingSource)
					yield return item;
			}
			set
			{
				itemsBindingSource.DataSource = new SortableDocProjectItemList(new List<DocProjectItemBase>(value));
			}
		}

		public MessageBoxOptions MessageBoxOptions
		{
			get
			{
				if (RightToLeft == RightToLeft.Yes)
					return MessageBoxOptions.RightAlign | MessageBoxOptions.RtlReading;
				else
					return (MessageBoxOptions) 0;
			}
		}
		#endregion

		#region Private / Protected
		private readonly string rootDirectory;
		#endregion

		#region Constructors
		/// <summary>
		/// Constructs a new instance of the <see cref="BuildItemsForm" /> class.
		/// </summary>
		public BuildItemsForm(string rootDirectory)
			: this()
		{
			if (string.IsNullOrEmpty(rootDirectory))
				throw new ArgumentException(Resources.Errors.NullOrEmpty, "rootDirectory");

			this.rootDirectory = rootDirectory;
		}

		/// <summary>
		/// Constructs a new instance of the <see cref="BuildItemsForm" /> class.
		/// </summary>
		private BuildItemsForm()
		{
			InitializeComponent();
		}
		#endregion

		#region Methods
		[System.Diagnostics.DebuggerHidden]
		private static Icon GetItemIcon(string path, bool isDirectory)
		{
			if (isDirectory)
				return Resources.Icons.Folder;

			string extension = System.IO.Path.GetExtension(path);

			if (string.IsNullOrEmpty(extension))
				return Resources.Icons.UnknownFile;
			else
			{
				try
				{
					return Icon.ExtractAssociatedIcon(path);
				}
				catch (System.IO.FileNotFoundException)
				{
					return Resources.Icons.UnknownFile;
				}
			}
		}
		#endregion

		#region Event Handlers
		private void itemsDataGridView_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
		{
			foreach (DataGridViewRow row in itemsDataGridView.Rows)
			{
				if (row.Index > -1)
				{
					DocProjectItemBase item = (DocProjectItemBase) row.DataBoundItem;

					if (item != null)
					{
						SystemPath path = item.Path;

						DataGridViewLinkCell pathCell = (DataGridViewLinkCell) row.Cells[pathColumn.Name];
						pathCell.ToolTipText = path;

						DataGridViewImageCell imageCell = (DataGridViewImageCell) row.Cells[imageColumn.Name];

						Icon icon = (path.HasWildcard)
							? Resources.Icons.UnknownFile
							: GetItemIcon(path.Path, item is DocProjectFolder);

						if (icon != null)
							imageCell.Value = new Bitmap(icon.ToBitmap(), 24, 24);

						DataGridViewTextBoxCell existsCell = (DataGridViewTextBoxCell) row.Cells[existsColumn.Name];
						existsCell.Value = path.Exists;
					}
				}
			}
		}

		[System.Diagnostics.DebuggerHidden]
		private void itemsDataGridView_CellContentClick(object sender, DataGridViewCellEventArgs e)
		{
			if (e.RowIndex > -1 && itemsDataGridView.Columns[e.ColumnIndex] == pathColumn)
			{
				DocProjectItemBase item = (DocProjectItemBase) itemsDataGridView.Rows[e.RowIndex].DataBoundItem;

				try
				{
					System.Diagnostics.Process.Start(item.Path);
				}
				catch (Win32Exception ex)
				{
					MessageBox.Show(this, ex.Message, Resources.Text.CannotOpenBuildItemCaption,
						MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1, MessageBoxOptions);
				}
				catch (ArgumentException ex)
				{
					MessageBox.Show(this, ex.Message, Resources.Text.CannotOpenBuildItemCaption,
						MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1, MessageBoxOptions);
				}
				catch (InvalidOperationException ex)
				{
					MessageBox.Show(this, ex.Message, Resources.Text.CannotOpenBuildItemCaption,
						MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1, MessageBoxOptions);
				}
			}
		}

		private void itemsDataGridView_CurrentCellChanged(object sender, EventArgs e)
		{
			itemsDataGridView.ClearSelection();
		}

		private void itemsDataGridView_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
		{
			if (e.RowIndex > -1 && e.ColumnIndex == pathColumn.Index)
			{
				e.Value = ((SystemPath) e.Value).CompleteRelativeTo(rootDirectory);
				e.FormattingApplied = true;
			}
		}

		private void BuildItemsForm_KeyPress(object sender, KeyPressEventArgs e)
		{
			if (e.KeyChar == (char) Keys.Escape)
				Close();
		}
		#endregion
	}
}